<?
// /ui/commandhandler/request/group|renew|restore/group/[groupname][/status/ok]

class News_Command_Bookmarker
{
    var $Controller;
    var $Group;
    var $Request;
    var $Index;
    var $Item;
    var $Up;
    var $Page;
    function News_Command_Bookmarker($controller)
	{
	    $this->Controller = $controller;
        $this->Group      = $this->Controller->Settings['group']; 
        $this->Index      = $this->Controller->Settings['index']; 
        $this->Page       = $this->Controller->Settings['page']; 
        $this->Request    = $this->Controller->Settings['request']; 
        $this->Up         = $this->Controller->Settings['up'];
        $this->Down       = $this->Controller->Settings['dn'];
        $this->Seek       = $this->Controller->Settings['seek'];
        $this->Path       = "notify/{$this->Group}.{$this->Index}.mark";
	    if ($this->Request=='markoff')
	    {
	        $this->Path .= 'off';
	    }
	    else if ($this->Request=='markedit')
	    {
	        $this->Path .= 'edit';
	    }
	    else if ($this->Request=='markopen')
	    {
	        $this->Path .= 'open';
	    }
	    else if ($this->Request=='markhilo')
	    { 
	        $this->Path = "notify/{$this->Group}.{$this->Index}.{$this->Page}-{$this->Down}-{$this->Up}.markhilo";
	    }
        $this->URI        = $this->Controller->URI (array('status'=>'ok'));
	}
	
	function GetURI()
	{
	    $uri = $this->Controller->URI (array('ui'=>'articlelist', 'request'=>NULL));
	    if ($this->Request=='markoff' || $this->Request=='markedit' || $this->Request=='markopen' || $this->Request=='markhilo')
	    {
	        $uri = $this->Controller->URI (array('ui'=>'bookmarks', 'request'=>NULL
	                                      , 'group'=>NULL, 'index'=>NULL));
	    } 

	    if ($this->Request=='markhilo')
	    {
	        $uri['view'] = 'hilo';
	    }

	    return array("RequestURI"  => $this->URI,
	                 "RedirectURI" => $uri);
	}
	
	function GetStatus()
	{
	    if (!file_exists($this->Path))
        {
            return 'OK';
        } 
        return '';
	}
	
	function Signal()
	{   
        $message = $this->Group;
        
        if (strlen($this->Controller->Settings['status']) > 0)
        {
            return $this->GetStatus ();
        } 
        
        if (isset($this->Controller->Settings['rec']) > 0)
        {
            $array = $this->Controller->Settings['rec'];
            $str   = array();
            $str[] = "group={$message}";
            for ($i=0;$i<sizeof($array);$i++)
            {
                $str[] = "rec[]=" . $array[$i];
            }
            $message = implode('&', $str);
        } 
        
        if (strlen($this->Controller->Settings['seek']) > 0)
        {
            $message = "groupname={$this->Group}&filename=data/find/{$this->Group}/{$this->Seek}.xml";
			if (isset($this->Up))
			{
	            $message .= "&up={$this->Up}&down={$this->Down}";
			}
        } 		
		else if (isset($this->Up))
		{
            $message = "groupname={$this->Group}&up={$this->Up}&down={$this->Down}&filename=data/{$this->Group}.xml";
		}
        if ($this->Request=='markopen')
        {
            $message ="group={$this->Group}&pagenum={$this->Page}";
        }
        $this->Controller->Save($this->Path, $message); 
        return $this->Controller->Dialog ($this->GetURI());
	}
	
	function Invoke($command)
	{  
	
	    $this->Path = $command['path']; echo $this->Path;
	    
		$up = NULL; $down = NULL;
        $groupname  = $this->Controller->Read($command['path']);  
        $pageindex  = $command['arg1']; 
        $filename   = "data/{$groupname}.xml"; 
		if (strpos($groupname, '&filename')) parse_str($groupname);
		
		
        $destfile   = "data/mark/{$groupname}.{$pageindex}.xml"; 
        $ret        = array('<?xml version="1.0" encoding="ISO-8859-1"?>', '<articles>');
        

	        if ($command['type']=='markhilo')
	        {
	            $itemindex = $command['arg2']; $tmp = explode ('-', $pageindex);
                    $destfile   = "data/mark/{$groupname}.{$itemindex}.xml"; 


                    echo "opening $destfile\n";
                    if (!file_exists($destfile))
                    {
                         echo "ERROR: FILE NOT FOUND.\n";
                         unlink ($this->Path); return;
                    }
                    $xml = simplexml_load_file($destfile);
		    $result  = $xml->xpath("//article");  

                    $dn = $tmp[1]; $up = $tmp[2]; $ret=array();
                    $matrix = $this->Controller->CreateHiLo ($dn, $up);
                    $result = $this->Controller->SortArticles ($result);

                     echo $dn . "x" . $up;
                     for($i=0;$i<sizeof($matrix);$i++)
                     {
                         $x = $matrix[$i];

                     	 echo " ---> {$x}\n";
                         $ret[]=$result[$x]; 
                     }
                     $this->Controller->Preload ($groupname, $ret, -2);   
            unlink ($this->Path);
            return; 
	        }


        if ( $command['type'] == 'markoff')
        {
            if (file_exists($destfile)) 
            {
		        $xml = simplexml_load_file($destfile);
		        $result  = $xml->xpath("//article");  
		        for ($i=0;$i<sizeof($result);$i++)
		        {
		            $name    = $result[$i]->id; 
                    $b64     = base64_encode ($name);
                    $picname = "data/temp/{$groupname}/{$b64}";
                    if (file_exists($picname)) 
                    {
                        unlink ($picname);
                    }
		        }
            }
            unlink ($destfile);
            unlink ($this->Path);
            return; 
        }
	    else if ($command['type']=='markopen')
	    {
	        parse_str($groupname);
	        $groupname  = $group; 
	        $destfile   = "data/mark/{$groupname}.{$pageindex}.xml"; 
            if (file_exists($destfile)) 
            {
		        $xml = simplexml_load_file($destfile);
		        $result  = $xml->xpath("//article");  
		        $this->Controller->Preload ($groupname, $result, $pagenum);
            }
            else echo "Could not find {$destfile}";
            unlink ($this->Path);
            return; 
        }
	    else if ($command['type']=='markedit')
	    {
	        parse_str($groupname);
	        $groupname  = $group; 
	        $destfile   = "data/mark/{$groupname}.{$pageindex}.xml"; 
            $ret[]      = "<group>{$groupname}</group>";
	        
            if (file_exists($destfile)) 
            {
		        $xml = simplexml_load_file($destfile);
		        $result  = $xml->xpath("//article"); 
		        foreach ($result as $article)
		        {   
		            if (array_search($article->id, $rec)===false)
		            {
		                var_dump ($article);
		                $ret[] = $article->asXML();
		            }
		            else
		            {
		                $b64     = base64_encode ($article->id);
                        $picname = "data/temp/{$groupname}/{$b64}";
                        if (file_exists($picname)) 
                        {
                            unlink ($picname);
                        }		            
		            }
		        }
	            $ret[] = "</articles>";
	            $this->Controller->Save ($destfile, implode("\n", $ret));
            }
            unlink ($this->Path);
	        return;
	    }
        
		
		 
		if ($up) 
		{ 
			$destfile   = "data/mark/{$groupname}.{$pageindex}.{$up}.{$down}.xml"; 
		}
		
        #$filename   = "data/{$groupname}.xml"; 
		
        $ret[]      = "<group>{$groupname}</group>";
        
        if (file_exists($filename)) 
        {
		    $xml = simplexml_load_file($filename);
		    $result  = $xml->xpath("//list/article[@index='{$pageindex}']/items/article"); 
		    uasort($result, 'cmpx');
			$e = 0;
            foreach ($result as $article)
            {
            	if ($up == NULL || ($up > $e && $down <= $e))
				{
			        $name=$article->id;
			        $value=$article->subject;  
	                $subj  =  iconv("ISO-8859-1", "UTF-8", $value);
	                $ret[] = sprintf("<article><id>%s</id><subject><![CDATA[%s]]></subject></article>", $name, htmlentities($subj)); 
	                echo $subj ."\n";
				}
				$e ++;
            }
	        $ret[] = "</articles>";
			echo "{$destfile}\n";
	        $this->Controller->Save ($destfile, implode("\n", $ret));
            unlink ($this->Path);
		    return;
        }   
        echo "\n\n *** ERROR: Could not find group file {$filename}\n";
	}
}



